package app.goods.info.services;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;

import app.goods.info.constants.UrlConstont;
import app.goods.info.services.domain.LoginSmsUserInfo;
import app.goods.info.services.domain.LoginUserInfo;
import app.goods.info.utils.HttpClientUtils;
import app.goods.info.utils.UrlUtils;

public class LoginTokenService {
	private static final Logger LOGGER = LoggerFactory.getLogger(LoginTokenService.class);

	public String getToken() {
		String userName = "13905242907";
		String passWord = "Hyl198555@";
		String reponseBody = getLoginResponse(buildLoginUserInfo(userName, passWord));
//		String reponseBody = "{\n" + "	\"code\":\"0\",\n" + "	\"errorInfo\":\"ErrorCode:0,ErrorMsg:\",\n"
//				+ "	\"message\":\"\",\n" + "	\"result\":{\n"
//				+ "		\"agreement_url\":\"https://www.sqstzc.com/h5/app/agreement.html\",\n"
//				+ "		\"latest_login_log\":{\n" + "			\"username\":\"18262842001\",\n"
//				+ "			\"source\":2,\n" + "			\"ip\":\"2409:8920:be00:f782:bcef:f1ff:fe54:8ad\",\n"
//				+ "			\"login_time\":\"2023-10-11 16:24:26\"\n" + "		},\n"
//				+ "		\"improve_information\":0,\n" + "		\"agreement_no\":2,\n" + "		\"needChange\":2,\n"
//				+ "		\"agreement_version\":\"1.0\",\n" + "		\"agree\":1,\n" + "		\"account\":{\n"
//				+ "			\"buyer_type\":2,\n" + "			\"buyer_type_name\":\"学校食堂(教育局直管)\",\n"
//				+ "			\"buyer_name\":\"宿迁市宿城区支口实验学校\",\n" + "			\"contacts_phone\":\"13773955601\",\n"
//				+ "			\"phone\":\"13773955601\",\n" + "			\"certificated\":0,\n"
//				+ "			\"area\":\"宿城\",\n" + "			\"buyer_no\":\"0FE4C91A4021D06641BFB51ED1A0DA62\",\n"
//				+ "			\"buyer_no_original\":219992667,\n" + "			\"user_name\":\"曹明玉\",\n"
//				+ "			\"master_user\":\"13773955601\"\n" + "		},\n"
//				+ "		\"token\":\"61424F70007436014CC72929A7E58972\"\n" + "	}\n" + "}\n" + "\n" + "";
		String token = JSONObject.parseObject(reponseBody).getJSONObject("result").getString("token");
		return token;
	}

	public String getLoginResponse(LoginUserInfo loginUserInfo) {
		String loginUrl = UrlUtils.getRequestUrl(UrlConstont.LOGIN_URL);
		return HttpClientUtils.post(loginUrl, JSONObject.toJSONString(loginUserInfo));

	}

	public String getSmsCodeResponse(LoginSmsUserInfo loginSmsUserInfo) {
		String sendSmsCodeUrl = UrlUtils.getRequestUrl(UrlConstont.SEND_SMS);
		sendSmsCodeUrl = sendSmsCodeUrl.concat("mobile=").concat(loginSmsUserInfo.getMobile())
				.concat("&device_uuid=88:10:8f:44:0c:bc").concat("&device_id=88:10:8f:44:0c:bc")
				.concat("&sms_code=Login2");
		return HttpClientUtils.post(sendSmsCodeUrl, JSONObject.toJSONString(loginSmsUserInfo));

	}

	public LoginUserInfo buildLoginUserInfo(String userName, String pasword) {
		LoginUserInfo loginUserInfo = new LoginUserInfo();
		loginUserInfo.setCaptchaCode("");
		loginUserInfo.setLoginType("2");
//		loginUserInfo.setMobile(mobile);
//		loginUserInfo.setPassword("Hyl198555@");
		loginUserInfo.setMobile(userName);
		loginUserInfo.setPassword(pasword);
		loginUserInfo.setSource("2");
		loginUserInfo.setSystemCode("BUYER_SYSTEM");
		loginUserInfo.setToken("");
		return loginUserInfo;
	}

	public LoginSmsUserInfo buildLoginBySmsUserInfo(String mobile, String captchaToken, String verifyCode) {
		LoginSmsUserInfo loginSmsUserInfo = new LoginSmsUserInfo();
		loginSmsUserInfo.setCaptchaCode(verifyCode);
		loginSmsUserInfo.setLoginType("1");
		loginSmsUserInfo.setCaptchaToken(captchaToken == null ? "" : captchaToken);
		loginSmsUserInfo.setPassword("");
		loginSmsUserInfo.setMobile(mobile);
//		loginUserInfo.setPassword(pasword);
		loginSmsUserInfo.setSource("2");
		loginSmsUserInfo.setSystemCode("BUYER_SYSTEM");
		loginSmsUserInfo.setToken("");
		return loginSmsUserInfo;
	}

	public static void main(String[] args) {
		LOGGER.info("Test info");
		LOGGER.error("Test error");
//		System.out.println(new LoginTokenService().getToken());
	}

	public String getLoginBySmsResponse(LoginSmsUserInfo loginSmsUserInfo) {
		String loginUrl = UrlUtils.getRequestUrl(UrlConstont.LOGIN_URL);
		return HttpClientUtils.post(loginUrl, JSONObject.toJSONString(loginSmsUserInfo));
	}

}
